home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 127 / PC Guia 127.iso / Software / Produtividade / OpenOffice.org 2.0.1 / openofficeorg4.cab / test_regex.py < prev    next >
Text File  |  2005-11-19  |  4KB  |  114 lines

  1. from test.test_support import verbose, sortdict
  2. import warnings
  3. warnings.filterwarnings("ignore", "the regex module is deprecated",
  4.                         DeprecationWarning, __name__)
  5. import regex
  6. from regex_syntax import *
  7.  
  8. re = 'a+b+c+'
  9. print 'no match:', regex.match(re, 'hello aaaabcccc world')
  10. print 'successful search:', regex.search(re, 'hello aaaabcccc world')
  11. try:
  12.     cre = regex.compile('\(' + re)
  13. except regex.error:
  14.     print 'caught expected exception'
  15. else:
  16.     print 'expected regex.error not raised'
  17.  
  18. print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
  19. prev = regex.set_syntax(RE_SYNTAX_AWK)
  20. print 'successful awk syntax:', regex.search('(a+)|(b+)', 'cdb')
  21. regex.set_syntax(prev)
  22. print 'failed awk syntax:', regex.search('(a+)|(b+)', 'cdb')
  23.  
  24. re = '\(<one>[0-9]+\) *\(<two>[0-9]+\)'
  25. print 'matching with group names and compile()'
  26. cre = regex.compile(re)
  27. print cre.match('801 999')
  28. try:
  29.     print cre.group('one')
  30. except regex.error:
  31.     print 'caught expected exception'
  32. else:
  33.     print 'expected regex.error not raised'
  34.  
  35. print 'matching with group names and symcomp()'
  36. cre = regex.symcomp(re)
  37. print cre.match('801 999')
  38. print cre.group(0)
  39. print cre.group('one')
  40. print cre.group(1, 2)
  41. print cre.group('one', 'two')
  42. print 'realpat:', cre.realpat
  43. print 'groupindex:', sortdict(cre.groupindex)
  44.  
  45. re = 'world'
  46. cre = regex.compile(re)
  47. print 'not case folded search:', cre.search('HELLO WORLD')
  48. cre = regex.compile(re, regex.casefold)
  49. print 'case folded search:', cre.search('HELLO WORLD')
  50.  
  51. print '__members__:', cre.__members__
  52. print 'regs:', cre.regs
  53. print 'last:', cre.last
  54. print 'translate:', len(cre.translate)
  55. print 'givenpat:', cre.givenpat
  56.  
  57. print 'match with pos:', cre.match('hello world', 7)
  58. print 'search with pos:', cre.search('hello world there world', 7)
  59. print 'bogus group:', cre.group(0, 1, 3)
  60. try:
  61.     print 'no name:', cre.group('one')
  62. except regex.error:
  63.     print 'caught expected exception'
  64. else:
  65.     print 'expected regex.error not raised'
  66.  
  67. from regex_tests import *
  68. if verbose: print 'Running regex_tests test suite'
  69.  
  70. for t in tests:
  71.     pattern=s=outcome=repl=expected=None
  72.     if len(t)==5:
  73.         pattern, s, outcome, repl, expected = t
  74.     elif len(t)==3:
  75.         pattern, s, outcome = t
  76.     else:
  77.         raise ValueError, ('Test tuples should have 3 or 5 fields',t)
  78.  
  79.     try:
  80.         obj=regex.compile(pattern)
  81.     except regex.error:
  82.         if outcome==SYNTAX_ERROR: pass    # Expected a syntax error
  83.         else:
  84.             # Regex syntax errors aren't yet reported, so for
  85.             # the official test suite they'll be quietly ignored.
  86.             pass
  87.             #print '=== Syntax error:', t
  88.     else:
  89.         try:
  90.             result=obj.search(s)
  91.         except regex.error, msg:
  92.             print '=== Unexpected exception', t, repr(msg)
  93.         if outcome==SYNTAX_ERROR:
  94.             # This should have been a syntax error; forget it.
  95.             pass
  96.         elif outcome==FAIL:
  97.             if result==-1: pass   # No match, as expected
  98.             else: print '=== Succeeded incorrectly', t
  99.         elif outcome==SUCCEED:
  100.             if result!=-1:
  101.                 # Matched, as expected, so now we compute the
  102.                 # result string and compare it to our expected result.
  103.                 start, end = obj.regs[0]
  104.                 found=s[start:end]
  105.                 groups=obj.group(1,2,3,4,5,6,7,8,9,10)
  106.                 vardict=vars()
  107.                 for i in range(len(groups)):
  108.                     vardict['g'+str(i+1)]=str(groups[i])
  109.                 repl=eval(repl)
  110.                 if repl!=expected:
  111.                     print '=== grouping error', t, repr(repl)+' should be '+repr(expected)
  112.             else:
  113.                 print '=== Failed incorrectly', t
  114.